<?php
!defined('IN_FRAME') && die('404 Page');
class m_manager extends Model {
    
    /* user_name,nick_name,password,group_name,email,phone,last_login,rec_status,created_at */
    private $strSessKey = 'manager';
    
    public function __construct() {
        $this->table = 't_manager';
        parent::__construct();
    }
    
    public function logined() {
        return (isset($_SESSION[$this->strSessKey]) && is_array($_SESSION[$this->strSessKey]) && count($_SESSION[$this->strSessKey]) > 0);
    }
    
    public function getSession() {
        $re = false;
        if ($this->logined()) {
            $re = $_SESSION[$this->strSessKey];
        }
        return $re;
    }
    
    public function checkLogin($n, $p) {
        $arrTemp = $this->getOne(array('user_name' => $n, 'password' => sha1($p), 'rec_status' => 1));
        if (is_array($arrTemp) && count($arrTemp) > 0) {
            $_SESSION[$this->strSessKey] = $arrTemp;
            $this->update(array('last_login' => date('Y-m-d H:i:s')), array('id' => $arrTemp['id']));
        }
        return $arrTemp;
    }
    
    public function logout() {
        unset($_SESSION[$this->strSessKey]);
    }
    
    public function isUserName($str) {
        $arrTemp = array();
        preg_match('/^[0-9a-z]{3,20}$/', $str, $arrTemp);
        return count($arrTemp) > 0;
    }
    
    public function isPassword($str) {
        $arrTemp = array();
        preg_match('/^[0-9a-zA-Z]{6,20}$/', $str, $arrTemp);
        return count($arrTemp) > 0;
    }
    
    public function isNickName($str) {
        $int = mb_strlen($str, 'utf8');
        return (!Helper::hasBadword($str) && $int >= 3 && $int <= 20);
    }
    
    public function isPhone($str) {
        $arrTemp = array();
        preg_match('/^[0-9\-\(\)]{7,30}$/', $str, $arrTemp);
        return count($arrTemp) > 0;
    }
    
    
}